package com.nutricia.biz.impl;

import com.nutricia.biz.convertor.MenuUnionPermissionConvertor;
import com.nutricia.biz.convertor.SystemPermissionConvertor;
import com.nutricia.common.api.SystemPermissionFacade;
import com.nutricia.common.api.request.*;
import com.nutricia.common.api.result.*;
import com.nutricia.common.lang.ServiceCallBack;
import com.nutricia.common.util.CheckUtil;
import com.nutricia.core.model.domain.SystemPermissionDO;

import io.swagger.annotations.Api;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 系统权限
 *
 * @author: huangsheng
 * @date: 2017/10/24 下午3:03
 */
@Api()
@Service("systemPermissionFacade")
public class SystemPermissionFacadeImpl extends AbstractFacadeImpl implements SystemPermissionFacade {

    /**
     * 获取某个菜单系统权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午2:31
     */
    public GetSystemPermissionRes getMenuPermission(@RequestBody GetMenuPermissionReq getMenuPermissionReq) {
        logger.info("获取某个菜单系统权限数据--getMenuPermission");
        final GetSystemPermissionRes result = new GetSystemPermissionRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {

                CheckUtil.isNotBlank(getMenuPermissionReq.getMenuId(), "menuId为空");
                logger.info("菜单ID = {}", getMenuPermissionReq.getMenuId());
            }

            @Override
            public void doService() throws Exception {

                List<SystemPermissionDO> systemPermissionDOS = systemPermissionService.getSystemPermission(getMenuPermissionReq.getMenuId());
                logger.info("获取菜单权限数量：{}", null != systemPermissionDOS ? systemPermissionDOS.size() : "菜单数据为空");

                result.setPermissionData(SystemPermissionConvertor.convertToSystemPermissionInfoss(systemPermissionDOS));
                result.setSuccess(true);
            }
        });
    }

    /**
     * 获取所有菜单联合菜单权限的数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午6:08
     */
    public GetMenuUnionPermissionRes getMenuUnionPermission(@RequestBody VoidRequest voidRequest) {
        logger.info("获取所有菜单联合权限的数据--getMenuUnionPermission");
        final GetMenuUnionPermissionRes result = new GetMenuUnionPermissionRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                result.setMenuAndPermissionData(MenuUnionPermissionConvertor.convertToMenuAndPermissionDoS(systemPermissionService.getMenuUnionPermissions()));
                result.setSuccess(true);
            }
        });
    }

    /**
     * 获取角色拥有的权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 下午9:27
     */
    public GetPermissionByRoleIdRes getPermissionByRoleId(@RequestBody GetPermissionByRoleIdReq getPermissionByRoleIdReq) {
        logger.info("获取所有菜单联合权限的数据--getPermissionByRoleId");
        final GetPermissionByRoleIdRes result = new GetPermissionByRoleIdRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(getPermissionByRoleIdReq.getRoleId(), "roleId为空");
                logger.info("获取角色ID为 {} 的权限", getPermissionByRoleIdReq.getRoleId());
            }

            @Override
            public void doService() throws Exception {
                List<String> permissionIds = systemPermissionService.getPermissionByRoleId(getPermissionByRoleIdReq.getRoleId());
                logger.info("获取到该角色的权限数量为：{}", permissionIds.size());
                result.setPermissionIds(permissionIds);
                result.setSuccess(true);
            }

        });

    }

    /**
     * 新增系统权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public CreateSystemPermissionRes createSystemPermission(@RequestBody CreateSystemPermissionReq createSystemPermissionReq) {
        logger.info("新增系统权限数据--createSystemPermission");
        final CreateSystemPermissionRes result = new CreateSystemPermissionRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(createSystemPermissionReq.getMenuId(), "menuId为空");
                CheckUtil.isNotBlank(createSystemPermissionReq.getPermissionCode(), "permissionId为空");
                CheckUtil.isNotBlank(createSystemPermissionReq.getPermissionName(), "permissionName为空");
            }

            @Override
            public void doService() throws Exception {

                SystemPermissionDO systemPermissionDO = new SystemPermissionDO();
                systemPermissionDO.setMenuId(createSystemPermissionReq.getMenuId());
                systemPermissionDO.setPermissionCode(createSystemPermissionReq.getPermissionCode());
                systemPermissionDO.setPermissionName(createSystemPermissionReq.getPermissionName());

                String permissionId = systemPermissionService.createSystemPermission(systemPermissionDO);
                logger.info("成功新增 ID= {} 的权限数据", permissionId);
                result.setPermissionId(permissionId);
                result.setSuccess(true);
            }

        });
    }

    /**
     * 更新系统权限数据
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public VoidResult updateSystemPermission(@RequestBody UpdateSystemPermissionReq updateSystemPermissionReq) {
        logger.info("更新系统权限数据--updateSystemPermission");
        final VoidResult result = new VoidResult();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {

                CheckUtil.isNotBlank(updateSystemPermissionReq.getPermissionId(),"permissionId为空");
                CheckUtil.isNotBlank(updateSystemPermissionReq.getPermissionCode(), "permissionCode为空");
                CheckUtil.isNotBlank(updateSystemPermissionReq.getPermissionName(),  "permissionName为空");
            }

            @Override
            public void doService() throws Exception {

                logger.info("更新：code = {} 的权限数据", updateSystemPermissionReq.getPermissionCode());
                SystemPermissionDO systemPermissionDO = new SystemPermissionDO();
                systemPermissionDO.setId(updateSystemPermissionReq.getPermissionId());
                systemPermissionDO.setPermissionCode(updateSystemPermissionReq.getPermissionCode());
                systemPermissionDO.setPermissionName(updateSystemPermissionReq.getPermissionName());

                systemPermissionService.updateSystemPermission(systemPermissionDO);
                result.setSuccess(true);
            }

        });
    }

    /**
     * 删除系统权限数据（逻辑删除）
     *
     * @author: huangsheng
     * @date: 2017/10/24 上午10:38
     */
    public VoidResult deleteSystemPermission(@RequestBody DeleteSystemPermissionReq deleteSystemPermissionReq) {
        logger.info("删除系统权限数据--deleteSystemPermission");
        final VoidResult result = new VoidResult();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                CheckUtil.isNotBlank(deleteSystemPermissionReq.getPermissionId(), "permissionId为空");
            }

            @Override
            public void doService() throws Exception {
                logger.info("删除：{} 的权限数据", deleteSystemPermissionReq.getPermissionId());

                systemPermissionService.deleteSystemPermission(deleteSystemPermissionReq.getPermissionId());
                result.setSuccess(true);
            }

        });
    }

}
